(function(){
    "use strict";
    app.controller('conCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','LANGUAGE',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,LANGUAGE) {
            $rootScope.moduleTitle = '容器管理 > 容器';//定义当前页
            // $rootScope.link = '/statics/css/user.css';//引入页面样式
            $scope.sites = [
                {site : "启动", val : "0"},
                {site : "关闭", val : "1"},
            ];
            $scope.param = {
                rows: 10,
                instanceNamespace: "default",
            };
            $scope.isbatchDelete = true;
            //获取主机列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows,
                    instanceNamespace: $scope.param.instanceNamespace,
                    instanceRename:$scope.searchByName,
                    appName:$scope.searchByApp,
                    instanceNetwork:$scope.searchByNet
                    };
                httpLoad.loadData({
                    url:'/deployer/getAllContainerInfoNew.do',
                    data: params,
                    success:function(data){
                        if(data.rows&&data.rows.length!=0){
                            $scope.conList = data.rows;
                            $scope.totalCount = data.records;
                            $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            $scope.getData(1);

            //跳转详情页
            $scope.detail = function(item){
                $state.go('app.containerManger.containerDetail',{conName:item,flag:0});
            };
            //日志
            $scope.log = function(item){  //打开模态
                var modalInstance,str=item.instanceCore.instanceRename;
                // var modalInstance,str="/abcs/data/local/localFileSystem/"+item.instanceCore.appName+'/'+item.instanceCore.instanceRename;
                modalInstance= $modal.open({
                    templateUrl : '/statics/tpl/containerManger/container/log.html',
                    windowClass: 'log-win',
                    controller : 'logModalCtrl',
                    resolve : {
                    	id: function() {
                            return  str;
                        }
                    }
                });
                modalInstance.result.then(function(){
                },function(){});
            };
            //监控
            $scope.monitor = function(id){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/containerManger/container/monitor.html',
                    controller : 'monitorModalCtrl',
                    resolve : {
                        id: function() {
                            return  id;
                        }
                    }
                });
                modalInstance.result.then(function(){
                },function(){});
            };
            //终端
            $scope.terminal = function(id){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/containerManger/container/terminal.html',
                    controller : 'terminalModalCtrl',
                    resolve : {
                        id: function() {
                            return  id;
                        }
                    }
                });
                modalInstance.result.then(function(){
                },function(){});
            };
        }
    ]);
    //日志ctrl
    angular.module('app').controller('logModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad','$sce',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad,$sce){ //依赖于modalInstance
                httpLoad.loadData({
                    url:'/logger/logger',
                    method:'POST',
                    data:{key: id},
                    success:function(rep){
                        if(rep.status==200){
                        	console.log(rep.url);
                        	$scope.logUrl=$sce.trustAsResourceUrl(rep.url);
                        }
                    }
                });
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
	app.controller('monitorModalCtrl', ['$rootScope', '$scope','$modal','$modalInstance', 'httpLoad','id', '$state','$stateParams','meterService',function($rootScope, $scope,$modal,$modalInstance, httpLoad,id,$state,$stateParams,meterService) {
		$scope.cancel = function() {
			$modalInstance.dismiss('cancel'); // 退出
		}
	}]);
    //终端ctrl
    angular.module('app').controller('terminalModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad){ //依赖于modalInstance
            httpLoad.loadData({
                url:'/container/terminal',
                method:'POST',
                data:{id: id},
                success:function(data){
                    if(data.success){
                        $scope.codeMirror.setValue(data.message);
                    }else{
                        $scope.pop(data.message);
                    }
                }
            });
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
    app.directive('ngCodeMirror1', [function () {
        return {
            restrict: 'EA',
            scope:{
                codeMirror: '='
            },
            link: function (scope, element, attrs) {
                var editor = $(element).find('#textarea1')[0];
                //初始化在线代码编辑器
                var codeMirror = CodeMirror.fromTextArea(editor, {
                    theme: 'erlang-dark',
                    // lineNumbers: true,
                    readOnly: true
                });
                scope.codeMirror = codeMirror;
            }
        }
    }]);
    app.directive('ngCodeMirror2', [function () {
        return {
            restrict: 'EA',
            scope:{
                codeMirror: '='
            },
            link: function (scope, element, attrs) {
                var editor = $(element).find('#textarea2')[0];
                //初始化在线代码编辑器
                var codeMirror = CodeMirror.fromTextArea(editor, {
                    theme: 'erlang-dark',
                    readOnly: true
                });
                scope.codeMirror = codeMirror;
            }
        }
    }]);
})();